package com.yunhe.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yunhe.model.Page;
import com.yunhe.model.UserModel;
import com.yunhe.service.IUserService;

public class UserAction extends ActionSupport implements ModelDriven<Object> {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IUserService userService;
	
	private UserModel user;
	private int pageSize;
	private int pageNum;
	private Page page;
	

	public IUserService getUserService() {
		return userService;
	}


	public void setUserService(IUserService userService) {
		this.userService = userService;
	}


	public UserModel getUser() {
		return user;
	}


	public void setUser(UserModel user) {
		this.user = user;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getPageNum() {
		return pageNum;
	}


	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}


	public Page getPage() {
		return page;
	}


	public void setPage(Page page) {
		this.page = page;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String queryLikeUser(){
		Map<String,Object> cond = new HashMap<String,Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		System.out.println("----------------->user age:"+user.getAge());
		cond.put("user", user);
		page=userService.queryLikeUser(cond);
		System.out.println(page.getResult().get(0));
		return "success";
	}
	/**
	 *增加用户
	 * @return
	 */
	public String addUser(){
		userService.addUser(user);
		
		return SUCCESS;
	}
	/**
	 * 删除用户
	 */
	public String delUser(){
		userService.delUser(user);
		
		return "success";
	}
   /**
    * 查找和编辑用户
    */
	public String findUser(){
		user=userService.getUser(user);
		ServletActionContext.getRequest().setAttribute("data", user);
		return "success";
	}
	public String editUser(){
		userService.editUser(user);
		
		return "success";
	}
	@Override
	public UserModel getModel() {
      if(user==null){
    	 user = new UserModel(); 
      }
		return user;
	}
	


}
